使用带菜单方法的Java ArrayList

您所在的位置:网站首页 java list添加 使用带菜单方法的Java ArrayList

使用带菜单方法的Java ArrayList

#使用带菜单方法的Java ArrayList| 来源: 网络整理| 查看: 265

可能有两个以上的:)

你的水果名单应该在你的节目“全球”因此它的内容可以通过viewAllFruit打印,并且可以添加新果在addFruit方法中。

现在,在两种方法中,您都会创建新列表,然后添加“defualt”结果,然后打印内容。当用户进入其选择时,您再次创建这些列表。

记住NEVER比较==或!=总是与equals()方法的字符串

下面是你的代码的一些 “干净” 版本。

public static void List initFruits() { ArrayList theFruit = new ArrayList(); theFruit.add("Plums"); theFruit.add("Grapes"); theFruit.add("Oranges"); theFruit.add("Prunes"); theFruit.add("Apples"); return theFruit; } public static void viewAllFruit(List fruits,String CustomerRef[]) { for (String fruit : fruits) { System.out.println("Fruit " + fruit + " is in stock "); } } public static void addFruit(List fruits,String CustomerRef[]) { Scanner input = new Scanner(System.in); String EE = "EE"; //you assked for EE as quit but you wanted to compare with "Goodbye!"; while(true) { System.out.println("Enter the fruits you'd like to add and EE to exit"); String choice = input.next(); if (choice.equals(EE)) { break; }; fruits.add(choice); } } public static void TheMenu() { String Customer[] = new String[10]; Scanner input = new Scanner(System.in); List fruits = initFruits(); String option; do { // loop back to here as long as Q isn't selected System.out.println("\nMenu"); System.out.println("V: Views all fruit"); System.out.println("A: To add a fruit to the list"); System.out.println("Q: To exit"); option = input.next(); if (option.charAt(0) == 'V') { viewAllFruit(fruits,Customer); } if (option.charAt(0) == 'A') { addFruit(fruits,Customer); } } while (option.charAt(0) != 'Q'); }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3